// Created by WXX on 2021/12/11 9:04
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class TopVotedCandidate {
public:
    vector<int> win;  // win[i]: times[i]时刻领先的候选人编号
    vector<int> times;

    TopVotedCandidate(vector<int> &persons, vector<int> &_times) {

        int n = persons.size();
        win.resize(n);
        times = _times;

        vector<int> sum(n);  // sum[i]: 编号为i的候选人当前票数
        int maxc = 0, maxp;
        for (int i = 0; i < n; i++) {
            int p = persons[i];
            if (++sum[p] >= maxc) {
                maxc = sum[p];
                maxp = p;
            }
            win[i] = maxp;
        }
    }

    int q(int t) {
        int k = upper_bound(times.begin(), times.end(), t) - times.begin() - 1;
        return win[k];
    }
};

int main() {

    vector<int> persons = {0, 1, 1, 0, 0, 1, 0};
    vector<int> times = {0, 5, 10, 15, 20, 25, 30};
    TopVotedCandidate *t = new TopVotedCandidate(persons, times);
    cout << t->q(3) << endl;  // 0
    cout << t->q(12) << endl;  // 1
    cout << t->q(25) << endl;  // 1
    cout << t->q(15) << endl;  // 0
    cout << t->q(24) << endl;  // 0
    cout << t->q(8) << endl;  // 1

    return 0;
}
